package com.itheima.mapper.store;

import com.itheima.domain.store.Member;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.domain.store.Traveller;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrdersMapper {

    //遍历
    @Select("select * from orders")
    @Results(id = "ordersMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "orderTime", property = "orderTimeStr"),
            @Result(column = "productId", property = "product", javaType = Product.class,
                    one = @One(select = "com.itheima.mapper.store.ProductMapper.findById")),
            @Result(column = "memberId", property = "member", javaType = Member.class,
                    one = @One(select = "com.itheima.mapper.store.OrdersMapper.findMByOid")),
            @Result(column = "id", property = "travellers", javaType = List.class,
                    many = @Many(select = "com.itheima.mapper.store.OrdersMapper.findTByOid"))
    })
    List<Orders> findAll();

    //id查找Orders
    @Select("select * from orders where id =#{id}")
    @ResultMap("ordersMap")
    Orders findById(String id);

    //id查找Member
    @Select("select * from member where id = #{id}")
    Member findMByOid(String id);


    //Oid查找traveller
    @Select("select * from traveller where id in (select travellerId from order_traveller where orderId = #{id})")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "credentialsType",property = "credentialsTypeStr")
    })
    List<Traveller> findTByOid(String id);


    //id查找关联表
//    @Select("select * from order_traveller where orderId=#{id}")
//    List<String> findOtIdsByOid(String id);

    //id删除order_traveller
    @Delete("delete from order_traveller where orderId=#{id}")
    void deleteOtByOid(String id);

    //Oid删除traveller
    @Delete("delete from traveller where id in (select travellerId from order_traveller where orderId = #{id})")
    void deleteTraByOid(String id);

    //orders表通过id删除
    @Delete("delete from orders where id=#{id}")
    void deleteOrdersByOid(String id);

    //模糊查询
    @Select("select * from orders where productId in ( select id from product where productName like #{id})")
    @ResultMap("ordersMap")
    List<Orders> findAllLike(String lms);
}
